<?php

class ArticleAction extends BaseAction
{
	public function __construct()
    {
		parent::__construct();
		$this->menuid = 12;
	}

	public function Index()
	{
		$article_mod = D('article');
		$article_cate_mod = D('article_cate');
		
		//搜索
		$where = '1=1';
		if (isset($_GET['keyword']) && trim($_GET['keyword'])) {
		    $where .= " AND title LIKE '%".$_GET['keyword']."%'";
		}
		if (isset($_GET['time_start'])) {
		    $time_start = strtotime($_GET['time_start']);
		    $where .= " AND add_time>=$time_start";
		}
		if (isset($_GET['time_end'])) {
		    $time_end = strtotime($_GET['time_end']);
		    $where .= " AND add_time<=$time_end";
		}
		if (isset($_GET['cate_id']) && intval($_GET['cate_id'])) {
		    $where .= " AND cate_id=".$_GET['cate_id'];
		}
		
		import("ORG.Util.Page");
		$count = $article_mod->where($where)->count();
		$p = new Page($count,12);
		$article_list = $article_mod->where($where)->limit($p->firstRow.','.$p->listRows)->order('ordid DESC')->findAll();
		
		$key = 1;
		foreach($article_list as $k=>$val){
			$article_list[$k]['key'] = ($p->nowPage-1) * ($p->listRows) + $key++;
			$article_list[$k]['cate_name'] = $article_cate_mod->field('name')->where('id='.$val['cate_id'])->find();
		}
		$result = $article_cate_mod->order('sort_order DESC')->findAll();
    	$cate_list = array();
    	foreach ($result as $val) {
    	    if ($val['pid']==0) {
    	        $cate_list['parent'][$val['id']] = $val;
    	    } else {
    	        $cate_list['sub'][$val['pid']][] = $val;
    	    }
    	}
		$sub_menu = $this->admin_menu($this->menuid,true);
		$page = $p->show();
		$this->assign('page',$page);
    	$this->assign('sub_menu',$sub_menu);
    	$this->assign('cate_list', $cate_list);
		$this->assign('article_list',$article_list);
		
		$this->display();
	}

	function edit()
	{
		$article_mod = D('article');
		if( isset($_GET['id']) ){
			$article_id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->error(L('please_select'));
		}
		$article_cate_mod = D('article_cate');
	    $result = $article_cate_mod->order('sort_order DESC')->findAll();
	    $cate_list = array();
	    foreach ($result as $val) {
	    	if ($val['pid']==0) {
	    	    $cate_list['parent'][$val['id']] = $val;
	    	} else {
	    	    $cate_list['sub'][$val['pid']][] = $val;
	    	}
	    }
		$article_info = $article_mod->where('id='.$article_id)->find();
    	$this->assign('cate_list',$cate_list);
		$this->assign('article',$article_info);
		$this->display();

	}

	function update()
	{
		$article_mod = D('article');
		$data = $article_mod->create();
		$data['add_time'] = time();
		if($data['cate_id']==0){
			$this->error('请选择文章分类');
		}
		$result = $article_mod->save($data);
		if(false !== $result){
			$this->success(L('operation_success'), '', '', 'edit');
		}else{
			$this->error(L('operation_failure'));
		}
	}

	function add()
	{
		$article_cate_mod = D('article_cate');
    	$result = $article_cate_mod->order('sort_order DESC')->findAll();
    	$cate_list = array();
    	foreach ($result as $val) {
    	    if ($val['pid']==0) {
    	        $cate_list['parent'][$val['id']] = $val;
    	    } else {
    	        $cate_list['sub'][$val['pid']][] = $val;
    	    }
    	}
    	$this->assign('cate_list',$cate_list);
    	$this->display();
	}

	function insert()
	{
		$article_mod = D('article');

		if($_POST['title']==''){
			$this->error(L('input').L('article_title'));
		}
		if(false === $data = $article_mod->create()){
			$this->error($article_mod->error());
		}
		//dump($data);exit;
		$data['add_time']=time();
		$result = $article_mod->add($data);
		if($result){
			$this->success(L('operation_success'));
		}else{
			$this->error(L('operation_failure'));
		}

	}


	function delete()
    {
		$article_mod = D('article');
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
            $this->error('请选择要删除的文章！');
		}
		if( isset($_POST['id'])&&is_array($_POST['id']) ){
			$cate_ids = implode(',',$_POST['id']);
			$article_mod->delete($cate_ids);
		}else{
			$cate_id = intval($_GET['id']);
		    $article_mod->where('id='.$cate_id)->delete();
		}
		$this->success(L('operation_success'));
    }

	function sort_order()
    {
    	$article_mod = D('article');
		if (isset($_POST['listorders'])) {
            foreach ($_POST['listorders'] as $id=>$sort_order) {
            	$data['ordid'] = $sort_order;
                $article_mod->where('id='.$id)->save($data);
            }
            $this->success(L('operation_success'));
        }
        $this->error(L('operation_failure'));
    }







}
?>